//
//  HMWelcomeViewController.swift
//  大深圳微博01
//
//  Created by shenzhenIOS on 16/4/11.
//  Copyright © 2016年 shenzhenIOS. All rights reserved.
//

import UIKit

import SnapKit
import SDWebImage
// Swift定义枚举, 使用原始值 0
enum Season: Int {
    case Spring = 0
    case Summer
    case Autumn
    case Winter
}

class HMWelcomeViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
//        let season = Season.Spring
//        let season2 = Season(rawValue: 250)
//        HMPrint("season2: \(season2)")

        
        prepareUI()
        setIcon()
        //判断数据加载有没有成功
        CZUseraccountViewModel.sharInatance.loadUserInfor { (error) in
            
            if error != nil {
            HMPrint("加载用户数据失败\(error)")
                return
        }
        
        self.setIcon()
        
        }
    }
    //设置欢迎界面的头像
    //TODO:这里出bug了  不会显示出图片和名字数据  304 status fobiden 
    
    
    func setIcon(){
    
        if let avatar_large = CZUseraccountViewModel.sharInatance.userAccount?.avatar_large {
        
//        let URL = NSURL(fileURLWithPath: avatar_large)
            let URL = NSURL(string: avatar_large)!
          self.iconView.sd_setImageWithURL(URL, placeholderImage: UIImage.init(named: "avatar_default_big"))
        
        }
    }
    private func prepareUI() {
        // 1.添加子控件
        view.addSubview(bkgImageView)
        view.addSubview(iconView)
        view.addSubview(welcomeLabel)
        
        // 2.添加约束
        bkgImageView.translatesAutoresizingMaskIntoConstraints = false
        iconView.translatesAutoresizingMaskIntoConstraints = false
        welcomeLabel.translatesAutoresizingMaskIntoConstraints = false
        
        // 背景 Masonry SnapKit
        bkgImageView.snp_makeConstraints { (make) -> Void in
            // make.的leading参照view的.leading: 注意参照的view的属性需要添加snp_
//            make.leading.equalTo(view.snp_leading)
//            make.top.equalTo(view.snp_top)
//            make.trailing.equalTo(view.snp_trailing)
//            make.bottom.equalTo(view.snp_bottom)
            
            // 缩写1,当约束的属性和参照的属性一样的时,参照的属性可以省略
//            make.leading.equalTo(view)
//            make.top.equalTo(view)
//            make.trailing.equalTo(view)
//            make.bottom.equalTo(view)
            
            // 缩写2:当参照都一样的时候可以连在一起写
//            make.leading.top.trailing.bottom.equalTo(view)
            
            // 填充父控件简单方式
//            make.edges.equalTo(UIEdgeInsets(top: 10, left: 10, bottom: -10, right: -10))
//            make.edges.equalTo(view)
            make.edges.equalTo(UIEdgeInsetsZero)
        }
        
        // 头像
        iconView.snp_makeConstraints { (make) -> Void in
//            make.centerX.equalTo(view.snp_centerX)
            make.centerX.equalTo(view)
            make.bottom.equalTo(view).offset(-160)
            make.size.equalTo(CGSize(width: 85, height: 85))
        }
        
        // label
        welcomeLabel.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(iconView)
            //解决一个坑
            make.top.equalTo(iconView.snp_bottom).offset(16)
        }
    }
    
    // 在view看的见的时候在做动画
    override func viewDidAppear(animated: Bool) {
        super.viewDidAppear(animated)
        
        iconViewAnimation()
    }
    
    /// 头像动画
    private func iconViewAnimation() {
        // 修改头像底部约束,动画
        // snp_updateConstraints: 修改约束
        iconView.snp_updateConstraints { (make) -> Void in
            make.bottom.equalTo(view).offset(-(UIScreen.mainScreen().bounds.height - 160))
        }
        
        // usingSpringWithDamping: 弹簧明显程度: 0 - 1, 数值越小越明显
        // initialSpringVelocity: 初速度
        UIView.animateWithDuration(1, delay:0, usingSpringWithDamping: 0.5, initialSpringVelocity: 5, options: UIViewAnimationOptions(rawValue: 0), animations: { () -> Void in
            // 通知系统来重新设置布局
            self.view.layoutIfNeeded()
            }) { (_) -> Void in
                // 让label显示出来
                UIView.animateWithDuration(HMDefautlAnimDuration, animations: { () -> Void in
                    self.welcomeLabel.alpha = 1
                    }, completion: { (_) -> Void in
                        HMPrint("动画完成了,要切换界面")
                        
                        //切换到转界面
                        (UIApplication.sharedApplication().delegate as!AppDelegate).switchViewcontroller(CZTabBarController())
                        HMPrint("切换到了主界面")
                })
        }

        
    }

    // MARK: - 懒加载
    /// 背景
    private lazy var bkgImageView: UIImageView = UIImageView(image: UIImage(named: "ad_background"))
    
    /// 头像
    private lazy var iconView: UIImageView = {
        let imageView = UIImageView(image: UIImage(named: "avatar_default_big"))
        
        // 圆角 170(2x) 85
        imageView.layer.cornerRadius = 42.5
        // 裁剪
        imageView.layer.masksToBounds = true
        
        return imageView
    }()
    
    /// 欢迎label
    private lazy var welcomeLabel: UILabel = {
        let label = UILabel()
        
        // 设置
        label.text = "欢迎归来"
        label.textColor = UIColor.blackColor()
        label.font = UIFont.systemFontOfSize(HMWelcomeLabelFontSize)
        label.sizeToFit()
        label.alpha = 0
        
        return label
    }()
}
